export const fieldLinkMixins = {
    data() {
        return {
            controlLinkArr: [],
            controlLinkArrDetail: [],
            orginData: {}
        }
    },
    mounted() {

    },
    methods: {

        getFieldLink(orgData) {
            let masterControlFiles = []
            let allFields = []
            let controlLinkArr = []

            for (let i = 0; i < orgData.head.length; i++) {
                allFields = [...allFields, ...orgData.head[i].groupvalue]
            }

            masterControlFiles = allFields.filter(item => {
                return item.sFormat.indexOf('fs_field') !== -1
            })

            const fsMasterArr = []

            masterControlFiles = masterControlFiles.map(item => {
                fsMasterArr.push(item.sField)
                const fsArr = item.sFormat.split(';').filter(item => {
                    return item.indexOf('fs_') !== -1
                }).map(item => item.split(':')).map(item => {
                    item[1] = item[1].split(',')
                    return item
                })
                return Object.fromEntries(fsArr)
            }).map((item, index) => {
                item.fs_master = new Array(item.fs_field.length)
                item.fs_master.fill(fsMasterArr[index])
                return item
            })

            for (let i = 0; i < masterControlFiles.length; i++) {
                if (Array.isArray(masterControlFiles[i].fs_field)) {
                    for (let j = 0; j < masterControlFiles[i].fs_field.length; j++) {
                        const obj = {}
                        for (let key in masterControlFiles[i]) {
                            obj[key] = masterControlFiles[i][key][j]
                        }
                        controlLinkArr.push(obj)
                    }
                } else {
                    controlLinkArr.push(masterControlFiles[i])
                }
            }

            // //console.log("****", controlLinkArr)
            // this.controlLinkArr = controlLinkArr

            return controlLinkArr
        },

        // 子表，暂时写成这样，以后再处理重复代码
        getFieldLinkDetail(orgData) {
            let masterControlFiles = []
            let allFields = []
            let controlLinkArr = []

            // 目前只处理单子表（第一个子表）
            for (let key in orgData.body) {
                allFields = [...orgData.body[key]]
                break
            }

            // 以下复制主表的逻辑
            masterControlFiles = allFields.filter(item => {
                return item.sFormat.indexOf('fs_field') !== -1
            })

            const fsMasterArr = []

            masterControlFiles = masterControlFiles.map(item => {
                fsMasterArr.push(item.sField)
                const fsArr = item.sFormat.split(';').filter(item => {
                    return item.indexOf('fs_') !== -1
                }).map(item => item.split(':')).map(item => {
                    item[1] = item[1].split(',')
                    return item
                })
                return Object.fromEntries(fsArr)
            }).map((item, index) => {
                item.fs_master = new Array(item.fs_field.length)
                item.fs_master.fill(fsMasterArr[index])
                return item
            })

            for (let i = 0; i < masterControlFiles.length; i++) {
                if (Array.isArray(masterControlFiles[i].fs_field)) {
                    for (let j = 0; j < masterControlFiles[i].fs_field.length; j++) {
                        const obj = {}
                        for (let key in masterControlFiles[i]) {
                            obj[key] = masterControlFiles[i][key][j]
                        }
                        controlLinkArr.push(obj)
                    }
                } else {
                    controlLinkArr.push(masterControlFiles[i])
                }
            }

            return controlLinkArr
        },

        handleFiledLink(masterObj) {
            // console.log("@@@@@@",masterObj)
            if (masterObj.ishead) { // 如果是主表，可以判断行号index为空，但还是判断ishead是防止场景动作出问题
                const filedLinkRule = this.controlLinkArr.filter(item => {
                    return item.fs_master == masterObj.sField
                })
                if (filedLinkRule.length > 0) {
                    //console.log('handleFiledLink_head', masterObj, this.controlLinkArr, filedLinkRule)
                    for (let i = 0; i < this.jsondata.head.length; i++) {
                        for (let j = 0; j < this.jsondata.head[i].groupvalue.length; j++) {
                            let linkedfield = []
                            filedLinkRule.forEach(fitem => {
                                if (this.jsondata.head[i].groupvalue[j].sField === fitem.fs_field && !linkedfield.includes(fitem.fs_field)) {
                                    // 保存已处理过的字段，后续不再处理该字段，否则会清除状态
                                    if (this.flLogicalJudgment(masterObj.value, fitem)) {
                                        this.jsondata.head[i].groupvalue[j].lCanInput = fitem.fs_disabled == '1' ? '0' : '1'
                                        this.jsondata.head[i].groupvalue[j].lCanOutput = fitem.fs_show == '1' ? '0' : '1'
                                        this.jsondata.head[i].groupvalue[j].lMustInput = fitem.fs_must
                                        // //console.log("^^^", this.jsondata.head[i].groupvalue[j])
                                        linkedfield.push(fitem.fs_field) // 保存已处理过的字段，后续不再处理该字段，否则会清除状态
                                    } else {
                                        //console.log("fitem%%%%", fitem)
                                        this.jsondata.head[i].groupvalue[j].lCanInput = this.orginData.head[i].groupvalue[j].lCanInput
                                        this.jsondata.head[i].groupvalue[j].lCanOutput = this.orginData.head[i].groupvalue[j].lCanOutput
                                        this.jsondata.head[i].groupvalue[j].lMustInput = this.orginData.head[i].groupvalue[j].lMustInput
                                        //隐藏时回显加载时的默认值
                                        if (fitem.fs_show === '1') {
                                            this.jsondata.head[i].groupvalue[j].value = this.orginData.head[i].groupvalue[j].value
                                            if (this.jsondata.head[i].groupvalue[j].valuehidden) {
                                                this.jsondata.head[i].groupvalue[j].valuehidden = this.orginData.head[i].groupvalue[j].valuehidden
                                            }
                                        }

                                    }
                                }
                            })
                        }
                    }

                    if (this.jsondata.bodydata) {
                        for (let bodydatakey in this.jsondata.bodydata) {
                            let body = this.jsondata.body[bodydatakey]
                            let orginbody = this.orginData.body[bodydatakey]
                            let bodydata = this.jsondata.bodydata[bodydatakey]
                            let orginbodydata = this.orginData.bodydata[bodydatakey]
                            for (let i = 0; i < bodydata.length; i++) {
                                for (let j = 0; j < bodydata[i].length; j++) {
                                    let linkedfield = []
                                    filedLinkRule.forEach(fitem => {
                                        if (bodydata[i][j].sField + '_b' === fitem.fs_field && !linkedfield.includes(fitem.fs_field)) {
                                            //console.log('字段联动', bodydata[i][j].sField, fitem.fs_field, masterObj.value, bodydata[i][j], fitem)
                                            if (this.flLogicalJudgment(masterObj.value, fitem)) {
                                                body[j].lCanInput = fitem.fs_disabled == '1' ? '0' : '1'
                                                body[j].lCanOutput = fitem.fs_show == '1' ? '0' : '1'
                                                body[j].lMustInput = fitem.fs_must
                                                bodydata[i][j].lCanInput = fitem.fs_disabled == '1' ? '0' : '1'
                                                bodydata[i][j].lCanOutput = fitem.fs_show == '1' ? '0' : '1'
                                                bodydata[i][j].lMustInput = fitem.fs_must
                                                linkedfield.push(fitem.fs_field)
                                            } else {
                                                // body[j].lCanInput = orginbodydata[j].lCanInput
                                                // body[j].lCanOutput = orginbodydata[j].lCanOutput
                                                // body[j].lMustInput = orginbodydata[j].lMustInput
                                                // bodydata[i][j].lCanInput = orginbodydata[j].lCanInput
                                                // bodydata[i][j].lCanOutput = orginbodydata[j].lCanOutput
                                                // bodydata[i][j].lMustInput = orginbodydata[j].lMustInput
                                                body[j].lCanInput = orginbody[j].lCanInput
                                                body[j].lCanOutput = orginbody[j].lCanOutput
                                                body[j].lMustInput = orginbody[j].lMustInput
                                                bodydata[i][j].lCanInput = orginbody[j].lCanInput
                                                bodydata[i][j].lCanOutput = orginbody[j].lCanOutput
                                                bodydata[i][j].lMustInput = orginbody[j].lMustInput

                                                //隐藏时恢复加载时的默认值
                                                if (fitem.fs_show === '1') {
                                                    bodydata[i][j].value = orginbodydata[i][j].value
                                                    if (bodydata[i][j].valuehidden) {
                                                        bodydata[i][j].valuehidden = orginbodydata[i][j].valuehidden
                                                    }
                                                }

                                            }
                                        }
                                    })
                                }
                            }
                        }
                    }
                }
            } else {
                const filedLinkRule = this.controlLinkArrDetail.filter(item => {
                    return item.fs_master == masterObj.sField
                })
                if (filedLinkRule.length > 0) {
                    //console.log('handleFiledLink', masterObj, this.controlLinkArrDetail, filedLinkRule)
                    if (this.jsondata.bodydata) {
                        for (let bodydatakey in this.jsondata.bodydata) {
                            let bodydata = this.jsondata.bodydata[bodydatakey]
                            let orginbodydata = this.orginData.body[bodydatakey]
                            let i = masterObj.index
                            // 因为只影响单行，所以不处理body只处理bodydata
                            // i是影响的行，等于传入的行号
                            // //console.log('bodydata[i]', bodydata[i])
                            for (let j = 0; j < bodydata[i].length; j++) {
                                let linkedfield = []
                                filedLinkRule.forEach(fitem => {
                                    if (bodydata[i][j].sField === fitem.fs_field && !linkedfield.includes(fitem.fs_field)) {
                                        //console.log('字段联动', bodydata[i][j].sField === fitem.fs_field, linkedfield, bodydata[i][j].sField, fitem.fs_field, masterObj.value, bodydata[i][j], fitem)
                                        if (this.flLogicalJudgment(masterObj.value, fitem)) {
                                            bodydata[i][j].lCanInput = fitem.fs_disabled == '1' ? '0' : '1'
                                            bodydata[i][j].lCanOutput = fitem.fs_show == '1' ? '0' : '1'
                                            bodydata[i][j].lMustInput = fitem.fs_must
                                            linkedfield.push(fitem.fs_field)
                                        } else {
                                            bodydata[i][j].lCanInput = orginbodydata[j].lCanInput
                                            bodydata[i][j].lCanOutput = orginbodydata[j].lCanOutput
                                            bodydata[i][j].lMustInput = orginbodydata[j].lMustInput
                                            //隐藏时恢复加载时的默认值
                                            if (fitem.fs_show === '1') {
                                                bodydata[i][j].value = orginbodydata[j].value
                                                if (bodydata[i][j].valuehidden) {
                                                    bodydata[i][j].valuehidden = bodydata[i][j].valuehidden
                                                }
                                            }
                                        }
                                    }
                                })
                            }
                        }
                    }
                }
            }
        },

        // 页面打开时初始化
        firstHandleFiledLink(orgData) {
            let headFields = []
            let detailLines = []

            for (let i = 0; i < orgData.head.length; i++) {
                headFields = [...headFields, ...orgData.head[i].groupvalue]
            }
            for (let key in orgData.bodydata) {
                detailLines = [...orgData.bodydata[key]]
                break
            }

            headFields.forEach(item => {
                // //console.log(item)
                this.handleFiledLink({ sField: item.sField, value: item.value, ishead: true, index: null })
            })

            // console.log('detailFields', detailFields)
            detailLines.forEach((itemLine, index) => {
                //console.log('itemLine', itemLine)
                itemLine.forEach(item => {
                    // //console.log('item, index', item, index)
                    this.handleFiledLink({ sField: item.sField, value: item.value, ishead: false, index: index })
                })
            })
        },

        flLogicalJudgment(masterValue, ruleObj) {
            let res = false
            // 等于
            if (ruleObj.fs_type === 'eq' && masterValue === ruleObj.fs_value) {
                res = true
            }
            // 包含判断
            if (ruleObj.fs_type === 'contain' && masterValue.indexOf(ruleObj.fs_value) !== -1) {
                res = true
            }

            // 长度大于
            if (ruleObj.fs_type === 'big' && masterValue.length > ruleObj.fs_value) {
                res = true
            }

            // 长度大于等于
            if (ruleObj.fs_type === 'bigeq' && masterValue.length >= ruleObj.fs_value) {
                res = true
            }

            // 长度等于
            if (ruleObj.fs_type === 'leneq' && masterValue.length == ruleObj.fs_value) {
                res = true
            }

            // 长度小于等于
            if (ruleObj.fs_type === 'smalleq' && masterValue.length <= ruleObj.fs_value) {
                res = true
            }

            // 长度小于
            if (ruleObj.fs_type === 'small' && masterValue.length < ruleObj.fs_value) {
                res = true
            }

            // 为空
            if (ruleObj.fs_type === 'isnull' && (!masterValue || masterValue.length == 0)) {
                res = true
            }

            return res
        }
    }
}
